import md5 from "./md5.min.js"

const appid="20230303001584204"
const key = "B35YkiLKMIuNWKQArNcW"
function translate(q,to,from = "auto"){
  const salt = Date.now()
  const sign = md5(`${appid}${q}${salt}${key}`)
  return new Promise((resolve,reject) =>{
    wx.showLoading({
      title: '翻译中',
    })
    wx.request({
      url: 'https://fanyi-api.baidu.com/api/trans/vip/translate',
      data:{
        q,
        from,
        to,
        appid,
        salt,
        sign
      },
      success: (result) => {
        if(result.data && result.data.trans_result){
          
          resolve(result.data)
        }
      },
      fail: (result) => {
        reject(result)
      },
    })
  })
}
module.exports = translate